package com.hzk.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzk.mapper.SeckillOrderMapper;
import com.hzk.pojo.SeckillOrder;
import com.hzk.pojo.UserBasic;
import com.hzk.service.ISeckillOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jack
 * @since 2022-03-01
 */
@Service
@Slf4j

public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {


    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author zhoubin
     * @since 1.0.0
     */
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取秒杀结果
     *
     * @param user
     * @param goodsId
     * @return
     */
    @Override

    public Long getResult(UserBasic user, Long goodsId) {
        //做一个
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new
                QueryWrapper<SeckillOrder>().eq("user_id", user.getId()).eq("goods_id",
                goodsId));
        if (null != seckillOrder){
            log.info(String.valueOf(seckillOrder.getOrderId()));
            return 1L;//成功
        } else {
            if (redisTemplate.hasKey("isStockEmpty:" + goodsId)) {
                return -1L;//失败
            } else {
                return 0L;//继续
            }
        }
    }

    @Override
    public SeckillOrder select(UserBasic userBasic) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrder>().eq("user_id", userBasic.getId()));
        return seckillOrder;
    }
}

